package logic

import (
	"context"
	"fmt"
	"yuni/model/mysql"

	"yuni/yunirpc/internal/svc"
	"yuni/yunirpc/yunirpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ModifierPasswordLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewModifierPasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ModifierPasswordLogic {
	return &ModifierPasswordLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ModifierPasswordLogic) ModifierPassword(in *yunirpc.ModifierPasswordRequest) (*yunirpc.ModifierPasswordResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User
	mysql.Db.Debug().Where("account =?", in.Account).Find(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("账号不存在")
	}
	if in.Password != in.NowPassword {
		return nil, fmt.Errorf("两次密码输入不一致")
	}
	if user.Password == in.Password {
		return nil, fmt.Errorf("新密码不能和原密码相同")
	}
	user.Password = in.Password
	mysql.Db.Debug().Updates(&user)

	return &yunirpc.ModifierPasswordResponse{
		UserId: int64(user.ID),
	}, nil
}
